home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / dsp / dspgroup / asms.arc / NEWSPECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-14  |  8.1 KB  |  361 lines

  1. #include    <stdio.h>
  2. #include    <dos.h>
  3. #include    <memory.h>
  4. #include    <process.h>
  5. #include    <conio.h>
  6. cdecl sigpts();
  7. #define    CONTROL 0x34
  8. #define    CONT2 0xb4
  9. #define COMLINE 62        /*  (f4)  */
  10. #define THRESH 63     /*   (f5)  */
  11. #define UP 72
  12. #define DOWN 80
  13. #define PGUP 73
  14. #define PGDOWN 81
  15. #define RIGHT 77
  16. #define LEFT 75  
  17. #define CNRIGHT 116
  18. #define CNLEFT 115  
  19. #define HOME 71
  20. #define END 79
  21. #define CNHOME 119
  22. #define CNEND 117
  23. #define CLOSE 61         /* (f3)   */
  24. #define TCONSTU 60       /* (f2)  */ 
  25. #define TCONSTD 59       /* (f1)  */ 
  26. #define TCONST_NEW 68    /* (f10) */
  27. #define GO inp(0x306)
  28. #define STOP inp(0x307)
  29. unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512];
  30. int spect_count,y[512],zh[512],threshold;
  31. long z[512];
  32. float norm[512],trate,freqmax;
  33. cdecl main(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {
  37. char c,scr_csts();
  38. int cmin,j,jh,k,i,scale,offset,kount;
  39. int ct,binmax;
  40. long count,jjj,jj,jjmax,jjmore,jjcnt;
  41. float zhold[513],zsum[256],tconst,rate,osc,amplitude;
  42.  
  43. seg320=0xd000;
  44. threshold=0;
  45. tconst=0.5;
  46. jjcnt=0;
  47. io320=0x300;            /*  base i/o address for 320 board */
  48. osc = 25e6/4.;          /*  basic board oscillator */
  49. cntrlpt = io320 + 7;
  50. prt0 = io320 + 4;
  51. prt2 = io320 + 6;
  52. scale = 5;
  53. jjmore = 0;
  54. offset = 180;
  55. clark: do {
  56.     cls();
  57.     printf("Input desired sample rate in Hz ");
  58.     scanf( "%f",&rate);
  59. } while ( (rate < 80.0 ) || (rate > 40000.1));
  60. /* count gives you the oscillator "divisor" just as in SIO's */
  61. count = osc/rate;
  62. cmin = osc /40000.0;
  63. if ( count < 2 ) count = 2;
  64. trate = osc /(float)count;
  65. printf("\nRate = %f ",trate);
  66. /* set timers for desired sampling rate */
  67. control_timer(count,osc,&trate);
  68. scr_rowcol(24,1);
  69. printf("%7.2f Hz     ",trate);
  70. for(k=0;k<20000;k++) {       /* time delay for 8254 & clear holding arrays */
  71.       
  72.     if (k<513) {
  73.         zhold[k]=0.;
  74.     if (k<256) norm[k]=0.;
  75.     }
  76. }     
  77.  
  78. STOP;   /* halt */
  79. /* Load the fft and square routines */
  80. spawnl(P_WAIT,"intel",argv[0],"ffthy","d000","300","0",NULL);
  81. /* Run it once to set up arrays and have proper values in the ports */
  82. GO; /* go */
  83. while(inp(io320)!=88) {};
  84. STOP; /* stop */
  85. /* Generate Reference Baseline Spectrum */
  86.     printf("\n How many samples in reference spectrum? ");
  87.     scanf("%d",&spect_count);
  88. ref_spect(spect_count);
  89. STOP;
  90. printf("\nReference spectrum --- Hit any character when ready");
  91. ci(); co80(); scr_rowcol(1,1);
  92.     printf("\n How many samples for each displayed spectrum? ");
  93.     scanf("%d",&spect_count);
  94.  
  95. /* Set it up to run forever until ^C or F3 to change screen and quit*/
  96. hres(); kount=0; 
  97. GO;
  98. while (1) 
  99. {
  100.     if ((c = scr_csts()) != 0 )
  101.         {
  102.          switch(c) {
  103.              case COMLINE: {
  104.                  co80();
  105.                  goto clark;
  106.                 break;
  107.             }
  108.             case THRESH: {
  109.                 co80();
  110.                 printf("Input threshold %d? ",threshold);
  111.                 scanf("%d",&threshold);
  112.                 if (threshold < 1) threshold=0;
  113.                 if (threshold > 199) threshold = 199;
  114.                 if (threshold==0) outp(97,inp(97)&0xFC);
  115.                 hres();
  116.                 break;
  117.             }
  118.             case UP: {
  119.                 scale -= 1;
  120.                 if (scale < 0) scale = 0;
  121.                 break;
  122.             }
  123.             case DOWN: {
  124.                 scale += 1;
  125.                 if (scale > 15) scale = 15;
  126.                 break;
  127.             }
  128.             case PGUP: {
  129.                 offset -= 10;
  130.                 if (offset < 0) offset = 0;
  131.                 break;
  132.             }
  133.             case PGDOWN: {
  134.                 offset += 10;
  135.                 if (offset > 180) offset = 180;
  136.                 break;
  137.             }
  138.             case CLOSE: {
  139.                 STOP; /* halt */
  140.                 co80();
  141.                 outp(97,inp(97)&0xFC);
  142.                 exit();
  143.                 break;
  144.             }
  145.             case HOME: {
  146.                 jjmore -= 1;
  147.                 if (jjmore < 0) jjmore = 0;
  148.                 break;
  149.             }
  150.             case END: {
  151.                 jjmore += 1;
  152.                 if (jjmore > 10000) jjmore = 10000;
  153.                 break;
  154.             }
  155.             case CNHOME: {
  156.                 jjmore /= 2;
  157.                 if (jjmore < 0) jjmore = 0;
  158.                 break;
  159.             }
  160.             case CNEND: {
  161.                 jjmore *= 2;
  162.                 if (jjmore > 10000) jjmore = 10000;
  163.                 break;
  164.             }
  165.                  case TCONSTU: {
  166.                 tconst *= 2.0;
  167.                 if (tconst>1.0) tconst=1.0;
  168.                 break;
  169.             }   
  170.             case TCONST_NEW: {
  171.                 co80(); printf("%3d\n",c);
  172.                 printf(" tconst currently = %9.7f --- Enter new value: ",tconst);
  173.                 scanf("%f",&tconst); cls();
  174.                 if (tconst>  1.0) tconst= 1.0;
  175.                 if (tconst<.0001) tconst=.0001 ;
  176.                 hres();
  177.                 break;
  178.             }
  179.                  case TCONSTD: {
  180.                 tconst *= 0.5;
  181.                 if (tconst<.0001) tconst=.0001 ;     
  182.                 break;
  183.             }   
  184.             default:
  185.                 break;
  186.         }
  187.     }
  188.  
  189. if (z[100]+z[110]+z[120]+z[130]+z[140]+z[150] > 30)  /* is there data? */
  190.   {          /* bypass the following loop if there isn't */
  191. for (k=0;k<256;k++) {
  192.          if (kount == 0) {
  193.             zsum[k]=z[k];
  194.         }
  195.         else zsum[k] += z[k];
  196. }    
  197. kount += 1;
  198. if (kount >= spect_count)   {   
  199.         kount=0;
  200.     for (k=0;k<256;k++) zsum[k] *= norm[k];        
  201.         amplitude=0.;
  202.         binmax=128;
  203.     for(k=0;k<512;k+=2)        {
  204.         zhold[k]=(1.0-tconst)*zhold[k] + zsum[k/2]*tconst;
  205.         y[k]= offset-(((int)zhold[k])>>scale);
  206.             zsum[k/2] = 0;
  207.             if (y[k] < 1) y[k]=1;                  /* don't go off the top */
  208.         if (k > 80 && zhold[k]>amplitude) {    /* avoid DC peak */
  209.             binmax=k;
  210.             amplitude=zhold[k];
  211.         }
  212.     }
  213.         for (k=1;k<511;k+=2) y[k]=(y[k-1]+y[k+1])/2.0;
  214.         scr_rowcol(24,1);
  215.         freqmax=((float)binmax)*trate/1024.0;
  216.     printf("Rate= %5.0f Hz Scale= %d tconst= % 5.4f sampl= %d freq=% 5.0f amp=% 4.0f  ",
  217.             trate,scale,tconst,spect_count,freqmax,amplitude);
  218.     sigpts(&y[0],512);      /* show old fft */
  219.     if (threshold!=0){
  220.         if (y[binmax]<threshold) {
  221.             outp(97,inp(97)&0xFC);
  222.             prt1=1193180.0/freqmax;
  223.             outp(67,182);
  224.             outp(66,prt1&0xFF);
  225.             outp(66,(prt1>>8)&0xFF);
  226.             outp(97,inp(97)|3);
  227.     }
  228.     else outp(97,inp(97)&0xFC);
  229.     }
  230.  
  231.    }
  232.   }     /* end of bypass loop -- display counter even if no data at input */
  233.       
  234.         scr_rowcol(1,1);  printf("% 5d   ",jjcnt++);
  235.         if (jjcnt>32675) jjcnt=0;
  236.  
  237.        fft();
  238.   }
  239. }
  240.  
  241. fft()
  242. {    
  243.     int k;
  244.     while(inp(io320)!=88) {};
  245.     STOP;  /* halt */
  246.     memcpy((char *)zh,(char *)0xD0001800L,512);
  247.     memcpy((char *)zl,(char *)0xD0001C00L,512);
  248.     GO; /* go */
  249.     for(k=0;k<256;k++) {
  250.         z[k]=zh[k];
  251.         z[k]=((z[k])<<16)+zl[k];
  252.     }
  253. }
  254.  
  255. co80()
  256. {
  257.     printf("\x1B[=3h");
  258. }
  259.  
  260. cls()
  261. {
  262.     printf("\x1B[2J");
  263. }
  264.  
  265. hres()
  266. {
  267.     printf("\x1b[=6h");
  268. }
  269.  
  270. scr_rowcol(x,y)
  271. unsigned x,y;
  272. {
  273.     union REGS inregs,outregs;
  274.     inregs.x.ax=0x200;
  275.     inregs.x.bx=15;
  276.     inregs.x.dx=(x<<8)+y;
  277.     int86(0x10,&inregs,&outregs);
  278. }
  279.  
  280. ci()
  281. {
  282.     int j;
  283.     while (kbhit()==0) {};
  284.       j=getch();
  285. }
  286.  
  287. char scr_csts()
  288. {
  289.     char c;
  290.     if (kbhit()==0) return(0);
  291.     if (getch()!=0) return(0);
  292.     return(getch());
  293. }
  294.  
  295. control_timer(cnt,osc,trate)
  296. long cnt;
  297. float *trate,osc;
  298. {
  299.     int ctl,cth;
  300.     /* tell board timer info coming */
  301.     outp(cntrlpt ,CONTROL);
  302.     /* count is timing divisor */
  303.     ctl = cnt & 0xff;    /* lo byte */
  304.     cth = ( cnt >> 8 ) & 0xff;/* hi byte */
  305.     /* control timer 0 */
  306.     outp(prt0 , ctl);
  307.     outp(prt0 , cth);
  308.     /* control timer 2 */
  309.     outp(cntrlpt , CONT2);
  310.     outp(prt2 , ctl);
  311.     outp(prt2 , cth);
  312.     /* compute new sampling rate  for output on screen*/
  313.     *trate = osc/(float)cnt;
  314. }
  315. ref_spect(spect_count)
  316. int spect_count;
  317. {
  318. float t1,t2,t3,t4,norm_max;
  319. int binmax,jj,k;
  320.  
  321.         GO; /* go */
  322. for(k=0;k<256;k++) norm[k] = 0.0;
  323. for (jj=0;jj<spect_count;jj++) {
  324.         scr_rowcol(4,1);printf("% 5d  ",jj);
  325.             fft();
  326.         for(k=0;k<256;k++) norm[k] += z[k];
  327.      }
  328. binmax=128;
  329. norm_max=-999.;
  330. t1 = t2 = t3 = t4 = 0.0;
  331. for (k=0;k<249;k++) {    /* start with 7-point running average smoothing */
  332.         t4=t3;
  333.     t3=t2;
  334.     t2=t1;
  335.     t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
  336.     norm[k]=t4/7.0;          /* keep centered at proper index */
  337.     
  338.        if ( norm[k] > norm_max ) {
  339.            binmax = k;
  340.            norm_max = norm[k] ;
  341.        }    
  342.    }
  343. for (k=0;k<6;k++) {
  344.        norm[k]=0.;
  345.        norm[256-k]=0.;
  346.      }  
  347. for (k=0;k<512;k+=2) {
  348.     y[k]=180 - (norm[k/2]/norm_max)*100.;
  349.     if ((norm[k/2]>norm_max/50.) && (k>60)) 
  350.                  {norm[k/2] = norm_max / norm[k/2]/256.0;}
  351.     else         norm[k/2] = 0.;
  352. }
  353. for (k=1;k<511;k+=2) y[k]=(y[k-1]+y[k+1])/2.0;
  354.         hres(); sigpts(&y[0],512);
  355.            scr_rowcol(24,1);
  356. printf("Rate = %7.2f Hz                          freq=% 7.1f amp=% 8.1f  ",
  357.             trate,         ((float)binmax)*trate/512.0,norm_max );
  358.  
  359. }
  360.  
  361.